//+------------------------------------------------------------------+
//|                                        Mn Show Profit Alerts.mq4 |
//+------------------------------------------------------------------+
//download source: http://www.forexfactory.com/showthread.php?p=7403504#post7403504
//mod. fxdaytrader (thousands separator, zerocolor, calcswapcommissionalso, profitpips, alerts ...
#property copyright "Mn // mod. fxdaytrader"
#property link "http://ForexBaron.net"

#property indicator_chart_window

extern bool FilterBySymbol      = FALSE;
extern bool FilterByMagicNumber = FALSE;
extern int  MagicNumber         = 0;
//
extern bool   CalcSwapCommissionAlso = true;//fxdaytrader
extern string ThousandsSeparator = " ";
extern string DecimalSeparator   = ",";
extern int    mCorn = 3, mtextSize = 14, mXDist = 10, mYDist = 10;
extern color  ProfitColor = Blue, LossColor = FireBrick, ZeroColor=Green;
extern string mFont = "Arial";
extern bool   ShowProfitPips       = TRUE;
extern color  PipsColor            = White;
extern color  PipsValueColor       = White;
extern int    AddTomYDist          = 20;
extern int    AddTomYDist2          = 20;
extern string PipsFont        = "Arial";
extern string PipsValueFont   = "Arial";
extern int    PipsTextSize = 12;
extern int    PipsValueTextSize = 12;
double profitpips;
extern string ahis = "******* ALERT SETTINGS:";
extern bool   AlertAtProfitOf        = true;
extern double AlertAtProfitAmount    = 1000.00;
extern bool   AlertAtLossOf          = true;
extern double AlertAtLossAmount      = 1000.00;
extern int    AlertCandle            = 1;
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFileProfit        = "alert.wav";
extern string SoundFileLoss          = "alert2.wav";
extern string LotSizeDisplay = "Lot size to display Pip Value";
extern double LotSize=1;   // Lot Size
extern string Account_Symbol = "$";
double point;
string SoundFileName;
int lastAlert=3;
int Multiplier;
double pips2dbl;
//+------------------------------------------------------------------+
   
int init()
 {  
   
   
   point=Point;
   if((Digits==3) || (Digits==5))
     {
      point*=10;
     }
 
  
 if (ShowProfitPips) {
   ObjectCreate("mShowProf2", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("mShowProf2", OBJPROP_CORNER, mCorn);
   ObjectSet("mShowProf2", OBJPROP_XDISTANCE, mXDist);
   ObjectSet("mShowProf2", OBJPROP_YDISTANCE, mYDist);
   ObjectSetText("mShowProf2", " ", mtextSize, mFont, CLR_NONE);
  }
  
  
  if (ShowProfitPips) {mYDist+=AddTomYDist;
   ObjectCreate("mShowProf1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("mShowProf1", OBJPROP_CORNER, mCorn);
   ObjectSet("mShowProf1", OBJPROP_XDISTANCE, mXDist);
   ObjectSet("mShowProf1", OBJPROP_YDISTANCE, mYDist);
   ObjectSetText("mShowProf1", " ", mtextSize, mFont, CLR_NONE);
  }
  
   
  
  if (ShowProfitPips) mYDist+=AddTomYDist2;
  ObjectCreate("mShowProf", OBJ_LABEL, 0, 0, 0, 0, 0);
  ObjectSet("mShowProf", OBJPROP_CORNER, mCorn);
  ObjectSet("mShowProf", OBJPROP_XDISTANCE, mXDist);
  ObjectSet("mShowProf", OBJPROP_YDISTANCE, mYDist);
  ObjectSetText("mShowProf", " ", mtextSize, mFont, CLR_NONE);
  
  AlertAtLossAmount = MathAbs(AlertAtLossAmount)*(-1);
  return(0); 
 }
 
//-----------------------------------------------+ 
int start()
 {
   double PipValue=(((MarketInfo(Symbol(),MODE_TICKVALUE)*point)/MarketInfo(Symbol(),MODE_TICKSIZE))*LotSize);
   color mTextCol;
   double mProf = 0;
   profitpips=0.0;
   for(int i = 0; i < OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
      //if(OrderSymbol() == Symbol() && OrderType() < 2)
      if(FilterBySymbol&&OrderSymbol()!=Symbol()) continue;
      if(FilterByMagicNumber&&OrderMagicNumber()!=MagicNumber) continue;
      if (OrderType() < 2)
        {
          if (!CalcSwapCommissionAlso) mProf = mProf + OrderProfit();
           else if (CalcSwapCommissionAlso) mProf = mProf + OrderProfit() + OrderSwap() + OrderCommission();
           
          if (ShowProfitPips) {
           BrokerDigitAdjust(OrderSymbol());
           if (OrderType()==OP_BUY) profitpips=profitpips+((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/pips2dbl);
           if (OrderType()==OP_SELL) profitpips=profitpips+((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/pips2dbl);
          }//if (ShowPips) {
        }
     }

   //mProf = -159651.62;//testing
   if(mProf < 0.00) { mTextCol = LossColor; SoundFileName = SoundFileLoss; }
    else if (mProf > 0.00) { mTextCol = ProfitColor; SoundFileName = SoundFileProfit; }
     else mTextCol = ZeroColor;
   //fxdaytrader
    string smProf = DoubleToStr(mProf,2);
   // smProf = StringReplace(smProf,".",DecimalSeparator);
    if (mProf>0.00) {
      if (lastAlert!=1) if (AlertAtProfitOf&&mProf>=AlertAtProfitAmount) { lastAlert=1; doAlerts("Profit >= "+DoubleToStr(AlertAtProfitAmount,2)+" (current profit: "+DoubleToStr(mProf,2)+") "+AccountCurrency(),SoundFileName); }
     if (mProf>=1000.00&&mProf<10000.00) smProf = StringInsert(smProf,1,ThousandsSeparator);
     if (mProf>=10000.00&&mProf<100000.00) smProf = StringInsert(smProf,2,ThousandsSeparator);
     if (mProf>=100000.00&&mProf<1000000.00) smProf = StringInsert(smProf,3,ThousandsSeparator);
     if (mProf>=1000000.00&&mProf<10000000.00) { smProf = StringInsert(smProf,1,ThousandsSeparator); smProf = StringInsert(smProf,5,ThousandsSeparator); }
     if (mProf>=10000000.00) { smProf = StringInsert(smProf,2,ThousandsSeparator); smProf = StringInsert(smProf,6,ThousandsSeparator); }
    }//if (mProf>0.00) {
    else if (mProf<0.00) {
      if (lastAlert!=2) if (AlertAtLossOf&&mProf<=AlertAtLossAmount) { lastAlert=2; doAlerts("Profit <= "+DoubleToStr(AlertAtLossAmount,2)+" (current loss: "+DoubleToStr(mProf,2)+") "+AccountCurrency(),SoundFileName); }
     if (MathAbs(mProf)>=1000.00&&MathAbs(mProf)<10000.00) smProf = StringInsert(smProf,2,ThousandsSeparator);
     if (MathAbs(mProf)>=10000.00&&MathAbs(mProf)<100000.00) smProf = StringInsert(smProf,3,ThousandsSeparator);
     if (MathAbs(mProf)>=100000.00&&MathAbs(mProf)<1000000.00) smProf = StringInsert(smProf,4,ThousandsSeparator);
     if (MathAbs(mProf)>=1000000.00&&MathAbs(mProf)<10000000.00) { smProf = StringInsert(smProf,2,ThousandsSeparator); smProf = StringInsert(smProf,6,ThousandsSeparator); }
     if (MathAbs(mProf)>=10000000.00) { smProf = StringInsert(smProf,3,ThousandsSeparator); smProf = StringInsert(smProf,7,ThousandsSeparator); }
    }//else if (mProf<0.00) {
   //end fxdaytrader

   //ObjectSetText("mShowProf", DoubleToStr(mProf, 2), mtextSize, mFont, mTextCol);
   ObjectSetText("mShowProf", smProf+" "+AccountCurrency(), mtextSize, mFont, mTextCol);//fxdaytrader
   if(mProf == 0) 
     //ObjectSetText("mShowProf", " ", mtextSize, mFont, CLR_NONE);
     ObjectSetText("mShowProf", "0" +" "+AccountCurrency(), mtextSize, mFont, mTextCol);//fxdaytrader
     
   if (ShowProfitPips) ObjectSetText("mShowProf1","Profit Pips: "+DoubleToStr(profitpips,2), PipsTextSize, PipsFont, PipsColor);//fxdaytrader
   if (ShowProfitPips) ObjectSetText("mShowProf2","Pip Value: "+Account_Symbol+DoubleToStr(PipValue,2),PipsValueTextSize, PipsValueFont, PipsValueColor);

  return(0);
 }
 
//-----------------------------------------------+
int deinit()
 {
  ObjectDelete("mShowProf");
  if (ShowProfitPips) ObjectDelete("mShowProf1");
  if (ShowProfitPips) ObjectDelete("mShowProf2");
  return(0);
 }

//-----------------------------------------------+

//String functions by Hanover, http://www.forexfactory.com/showthread.php?t=165557
//+------------------------------------------------------------------+
string StringInsert(string str1, int pos, string str2)   {
//+------------------------------------------------------------------+
// Creates a result where str2 is inserted into str1, at pos in str1, e.g.
// StringInsert("0123456789",5,"XY") returns "01234XY56789"

  string outstr = StringSubstr(str1,0,pos) + str2 + StringSubstr(str1,pos);
  return(outstr);
} 

//+------------------------------------------------------------------+
//string StringReplace(string str, string str1, string str2)  {
//+------------------------------------------------------------------+
// Usage: replaces every occurrence of str1 with str2 in str
 // string outstr = "";
  //for (int i=0; i<StringLen(str); i++)   {
   // if (StringSubstr(str,i,StringLen(str1)) == str1)  {
  //    outstr = outstr + str2;
 //     i += StringLen(str1) - 1;
  //  }
  //  else
 //     outstr = outstr + StringSubstr(str,i,1);
  //}
 // return(outstr);
//}

void doAlerts(string msg,string SoundFile) {
        msg="Show Profit -> Alert on "+Symbol()+", period "+TFtoStr(Period())+": "+msg;
 string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
  if (PopupAlerts) Alert(msg);
  if (EmailAlerts) SendMail(emailsubject,msg);
  if (PushNotificationAlerts) SendNotification(msg);
  if (SoundAlerts) PlaySound(SoundFile);

}//void doAlerts(string msg,string SoundFile) {

string TFtoStr(int period) {
 if (period==0) period=Period();
 switch(period) {
  case 1     : return("M1");  break;
  case 5     : return("M5");  break;
  case 15    : return("M15"); break;
  case 30    : return("M30"); break;
  case 60    : return("H1");  break;
  case 240   : return("H4");  break;
  case 1440  : return("D1");  break;
  case 10080 : return("W1");  break;
  case 43200 : return("MN1"); break;
  default    : return(DoubleToStr(period,0));
 }
 return("UNKNOWN");
}//string TFtoStr(int period) {

void BrokerDigitAdjust(string symbol) {
 Multiplier = 1;
 if (MarketInfo(symbol,MODE_DIGITS) == 3 || MarketInfo(symbol,MODE_DIGITS) == 5) Multiplier = 10;
 if (MarketInfo(symbol,MODE_DIGITS) == 6) Multiplier = 100;   
 if (MarketInfo(symbol,MODE_DIGITS) == 7) Multiplier = 1000;
 pips2dbl = Multiplier*MarketInfo(symbol,MODE_POINT);
 //Slippage*=Multiplier;
}

